package com.jianshuge.app.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jianshuge.app.R;
import com.jianshuge.app.bean.BookItem;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;

import com.jianshuge.app.common.BitmapManager;
import com.jianshuge.app.common.StringUtils;
import com.jianshuge.app.common.UIHelper;

public class ListViewBookAdapter extends MyBaseAdapter {

	private Context 					context;//运行上下文
	private List<BookItem> 				listItems;//数据集合
	private LayoutInflater 				listContainer;//视图容器
	private int 						itemViewResource;//自定义项视图源 
	private BitmapManager 				bmpManager;
	
	static class ListItemView { // 自定义控件集合
		public ImageView book_image;  
		private TextView book_title;
		private TextView book_author;
		private TextView book_rating;
		private TextView book_pubdate;
		private TextView book_publisher;
		private TextView book_isbn;
	}

	/**
	 * 实例化Adapter
	 * 
	 * @param context
	 * @param data
	 * @param resource
	 * @param isFaceClickEnable
	 */
	public ListViewBookAdapter(Context context, List<BookItem> data,int resource) {
		this.context = context;			
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
	}

	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	
	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d("method", "getView");
		
		//自定义视图
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			//获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			//获取控件对象
			listItemView.book_image = (ImageView)convertView.findViewById(R.id.book_listitem_image);
			listItemView.book_title = (TextView)convertView.findViewById(R.id.book_listitem_title);
			listItemView.book_author = (TextView)convertView.findViewById(R.id.book_listitem_author);
			listItemView.book_publisher = (TextView)convertView.findViewById(R.id.book_listitem_publisher);
			listItemView.book_pubdate = (TextView)convertView.findViewById(R.id.book_listitem_pubdate);	
			listItemView.book_isbn = (TextView)convertView.findViewById(R.id.book_listitem_isbn);
			listItemView.book_rating = (TextView)convertView.findViewById(R.id.book_listitem_rating);
			
			//设置控件集到convertView
			convertView.setTag(listItemView);
		}else {
			listItemView = (ListItemView)convertView.getTag();
		}	
		
		//设置文字和图片
		BookItem book_item = listItems.get(position);
		
		String book_image_url = book_item.getBookImageUrl();
		if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
			listItemView.book_image.setImageResource(R.drawable.default_book_pic);
		}else{
		//	bmpManager.loadBitmap(book_image_url, listItemView.book_image );
			UIHelper.showUserFace(listItemView.book_image, book_image_url);
		}
		
		listItemView.book_image.setOnClickListener(imageClickListener);
		listItemView.book_image.setTag(book_image_url);
	
		listItemView.book_title.setText("书名： " +  book_item.getTitle());
		listItemView.book_title.setTag( book_item );//设置隐藏参数(实体类)
		
		listItemView.book_author.setText("作者： " + book_item.getAuthor());
		listItemView.book_publisher.setText("出版社： " + book_item.getPublisher());
		listItemView.book_pubdate.setText("出版日期：" + book_item.getPubdate());
		listItemView.book_isbn.setText("ISBN： " + book_item.getIsbn());
		listItemView.book_rating.setText("豆瓣评分： " + book_item.getBookRating());
		
		return convertView;
	}
	
	private View.OnClickListener imageClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			UIHelper.showImageDialog(v.getContext(), (String)v.getTag());			
		}
	};
}
